home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ply15dat.zip / HILBERT.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  6KB  |  219 lines

  1. /*
  2.  * hilbert.c - Create a hilbert curve of spheres and cylinders
  3.  *
  4.  * Version:  (1.0) 30 January 1992
  5.  * Author:  Alexander Enzmann
  6.  *
  7.  * SIZE_FACTOR determines the number of components output.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include "def.h"
  14. #include "lib.h"
  15.  
  16. #define SIZE_FACTOR 5
  17.  
  18. static char *txname;
  19. static double last_x, last_y, x, y;
  20. static double offset_x, offset_y;
  21.  
  22. static void a(int i, double h);
  23. static void b(int i, double h);
  24. static void c(int i, double h);
  25. static void d(int i, double h);
  26.  
  27.  
  28. static void
  29. plot(double x, double y, double h)
  30. {
  31.    COORD4 c0, c1;
  32.  
  33.    /* cylinder from (last_x, last_y) -> (x, y) */
  34.    SET_COORD4(c0, last_x+offset_x, 0.0, last_y+offset_y, h/4.0);
  35.    SET_COORD4(c1, x+offset_x, 0.0, y+offset_y, h/4.0);
  36.    lib_output_cylcone(&c0, &c1, OUTPUT_CURVES);
  37.  
  38.    /* sphere at (x, y) */
  39.    lib_output_sphere(&c1, OUTPUT_CURVES);
  40.  
  41.    /* Reset last position */
  42.    last_x = x;
  43.    last_y = y;
  44. }
  45.  
  46. static void
  47. a(int i, double h)
  48. {
  49.    if (i>0) {
  50.       d(i-1, h); x -= h; plot(x, y, h);
  51.       a(i-1, h); y -= h; plot(x, y, h);
  52.       a(i-1, h); x += h; plot(x, y, h);
  53.       b(i-1, h);
  54.       }
  55. }
  56.  
  57. static void
  58. b(int i, double h)
  59. {
  60.    if (i>0) {
  61.       c(i-1, h); y += h; plot(x, y, h);
  62.       b(i-1, h); x += h; plot(x, y, h);
  63.       b(i-1, h); y -= h; plot(x, y, h);
  64.       a(i-1, h);
  65.       }
  66. }
  67.  
  68. static void
  69. c(int i, double h)
  70. {
  71.    if (i>0) {
  72.       b(i-1, h); x += h; plot(x, y, h);
  73.       c(i-1, h); y += h; plot(x, y, h);
  74.       c(i-1, h); x -= h; plot(x, y, h);
  75.       d(i-1, h);
  76.       }
  77. }
  78.  
  79. static void
  80. d(int i, double h)
  81. {
  82.    if (i>0) {
  83.       a(i-1, h); y -= h; plot(x, y, h);
  84.       d(i-1, h); x -= h; plot(x, y, h);
  85.       d(i-1, h); y += h; plot(x, y, h);
  86.       c(i-1, h);
  87.       }
  88. }
  89.  
  90. static void
  91. create_hilbert(int depth, double width)
  92. {
  93.    COORD4 center;
  94.    int i = 0;
  95.    double h = width;
  96.  
  97.    for (i=0;i<depth;i++)
  98.       h /= 2.0;
  99.  
  100.    /* Output a sphere at the start of the curve */
  101.    offset_x = offset_y = width / 2.0;
  102.    SET_COORD4(center, offset_x, 0.0, offset_y, h/4.0) ;
  103.    lib_output_sphere(¢er, OUTPUT_CURVES);
  104.    last_x = last_y = 0.0;
  105.  
  106.    /* Crank out the rest of the curve */
  107.    a(depth, h);
  108. }
  109.  
  110. static void
  111. create_textured_plane()
  112. {
  113.    printf("define test_map\n");
  114.    printf("   color_map(\n");
  115.    printf("      [0,   0.1, red,     orange]\n");
  116.    printf("      [0.1, 0.3, orange,  blue]\n");
  117.    printf("      [0.3, 0.5, blue,    skyblue]\n");
  118.    printf("      [0.5, 0.7, skyblue, orange]\n");
  119.    printf("      [0.7, 0.9, orange,  magenta]\n");
  120.    printf("      [0.9, 1.0, magenta, red],\n");
  121.    printf("      <1, 1, 1>)\n\n");
  122.    printf("# Simple color map texture\n");
  123.    printf("define noise_texture\n");
  124.    printf("texture {\n");
  125.    printf("   special surface {\n");
  126.    printf("      color test_map[noise(P)]\n");
  127.    printf("      ambient 0.2\n");
  128.    printf("      diffuse 0.8\n");
  129.    printf("      specular white, 0.5\n");
  130.    printf("      microfacet Reitz 10\n");
  131.    printf("      reflection white, 0.2\n");
  132.    printf("      }\n");
  133.    printf("   scale <0.3, 0.3, 0.3>\n");
  134.    printf("   }\n\n");
  135.    printf("define marble_turb 4\n");
  136.    printf("define marble_fn  (sawtooth(P[0] +");
  137.    printf(" marble_turb * noise(P,8)) + 1) / 2\n\n");
  138.    printf("define white_marble_map\n");
  139.    printf("   color_map(\n");
  140.    printf("      [0.0, 0.8, <1.0, 1.0, 1.0>, <0.7, 0.7, 0.7>]\n");
  141.    printf("      [0.8, 1.0, <0.7, 0.7, 0.7>, <0.1, 0.1, 0.1>],\n");
  142.    printf("      white)\n\n");
  143.    printf("# Simple marble textures\n");
  144.    printf("define white_marble\n");
  145.    printf("texture {\n");
  146.    printf("   special surface {\n");
  147.    printf("      color white_marble_map[marble_fn]\n");
  148.    printf("      ambient 0.2\n");
  149.    printf("      diffuse 0.8\n");
  150.    printf("      specular white, 0.5\n");
  151.    printf("      microfacet Reitz 10\n");
  152.    printf("      reflection white, 0.2\n");
  153.    printf("      }\n");
  154.    printf("   scale <0.6, 0.6, 0.6>\n");
  155.    printf("   }\n\n");
  156.    printf("object {\n");
  157.    printf("   polygon 4, <-20, -0.4, -20>, <-20, -0.4, 20>,\n");
  158.    printf("              < 20, -0.4,  20>, < 20, -0.4,-20>\n");
  159.    printf("   texture {\n");
  160.    printf("      hexagon white_marble, txt000, noise_texture\n");
  161.    printf("      scale <0.2, 0.2, 0.2>\n");
  162.    printf("      }\n");
  163.    printf("   }\n\n");
  164. }
  165.  
  166. void
  167. main(int argc, char *argv[])
  168. {
  169.     COORD4  back_color, curve_color ;
  170.     COORD4  center_pt, light, dir;
  171.     COORD4  from, at, up, msphere;
  172.     double offset = 1.0;
  173.     int i;
  174.  
  175.    /* We are using Polyray */
  176.    lib_set_raytracer(OUTPUT_POLYRAY);
  177.  
  178.     /* output viewpoint */
  179.     SET_COORD(from, 0.0, 5.0,-3.0) ;
  180.     SET_COORD(at,   0.0, 0.0, 0.0) ;
  181.     SET_COORD(up,   0.0, 1.0, 0.0) ;
  182.     lib_output_viewpoint( &from, &at, &up, 30.0, 1.0, 1.0, 256, 256);
  183.  
  184.     /* output background color - dark blue */
  185.     SET_COORD( back_color, 0.039, 0.18, 0.376 ) ;
  186.     lib_output_background_color( &back_color ) ;
  187.  
  188.     /* output light source */
  189.     SET_COORD4( light,-5.0, 10.0,-20.0, 0.5) ;
  190.     lib_output_light( &light ) ;
  191.     SET_COORD4( light, 5.0, 10.0, 20.0, 0.5) ;
  192.     lib_output_light( &light ) ;
  193.  
  194.     /* Make some mirrored spheres below the hilbert curve */
  195.     SET_COORD(curve_color, 1.0, 1.0, 1.0 ) ;
  196.     lib_output_color(&curve_color, 0.1, 0.2, 0.8, 0.2, 5.0, 0.0, 0.0);
  197.     
  198.     for (i=1;i<SIZE_FACTOR;i++)
  199.        offset /= 2.0;
  200.     SET_COORD4(msphere,offset-1.0,-0.2, offset-1.0, 0.2);
  201.     lib_output_sphere(&msphere, OUTPUT_CURVES);
  202.     SET_COORD4(msphere,offset-1.0,-0.2, 1, 0.2);
  203.     lib_output_sphere(&msphere, OUTPUT_CURVES);
  204.     SET_COORD4(msphere,1,-0.2, offset-1.0, 0.2);
  205.     lib_output_sphere(&msphere, OUTPUT_CURVES);
  206.     SET_COORD4(msphere,1,-0.2, 1, 0.2);
  207.     lib_output_sphere(&msphere, OUTPUT_CURVES);
  208.  
  209.     /* Dump out a plane with hexagonal tiling */
  210.     create_textured_plane();
  211.  
  212.     /* output color - red */
  213.     SET_COORD(curve_color, 1.0, 0.2, 0.2 ) ;
  214.     txname = lib_output_color(&curve_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0) ;
  215.  
  216.     /* compute and output hilbert curve */
  217.     create_hilbert(SIZE_FACTOR, 2.0);
  218. }
  219.